<%@page import="interfaces.DALException"%>
<%@page import="java.util.List"%>
<%@page import="dto_objects.*"%>
<%@page import="java.util.ArrayList"%>
<%@page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>

<% 
String login = "<form action=\"LoginServlet\">"+
		"Username:<br>"+
		"<input type=\"text\" name=\"un\"><br>"+
		"Password:<br>"+
		"<input type=\"text\" name=\"pw\">"+
		"<input type=\"submit\" value=\"log In\">"+
		"&nbsp;"+
		"</form>";
%>

<script type="text/javascript" src="../javascripts/jquery-latest.pack.js"></script>
<script type="text/javascript" src="../javascripts/popup.js"></script>

<jsp:useBean id="func" class="Function.FunctionLIBRARY" scope="session"/>  

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
	<title>Create Book</title>
	<link rel="stylesheet" type="text/css" href="../NewStyle.css">
</head>
<body>
	<div id=topBox>
			<div id=searchField>
				<form action="../Web/MainPage.jsp">
				<input type="text" name="searchText"> 
				<input type="submit" value="">
				<a id="aSearchButton" href="../Search/aSearch.jsp">Advanced search</a>
				</form>			
			</div>
	
			<div id=login>
				<%out.print(func.getClass().getName());
				if(session.getAttribute("currentSessionUser") == null){
					session.setAttribute("userMENU", null);
					out.print("<a class=myButton href=\"#loginField\" name=\"loginFieldClick\">Login</a>");
				}else{
					out.print("<form action=\"../LoginServlet\">"+
							"<input class=myButton type=\"submit\" value=\"Log Out\" name=\"logout\">"+
							"</form>");
				}
				%>
				
				
			</div>
	</div>
	<div id=main>
		<div id=header>

		</div>
		<div id=content>
			<div id=data>
			<form method="GET">
					<%
if (request.getMethod().equals("GET") && request.getParameter("ISBN") != null) {
	if(request.getParameter("action").equalsIgnoreCase("Submit")){
		int ISBN = Integer.parseInt(request.getParameter("ISBN"));
		int publisherID = Integer.parseInt(request.getParameter("publisherID"));
		String title = request.getParameter("title");
		int pages = Integer.parseInt(request.getParameter("pages"));
		int authorID = Integer.parseInt(request.getParameter("authorID"));
		int subjectID = Integer.parseInt(request.getParameter("subjectID"));
		PublisherDTO publisher = new PublisherDTO(publisherID,"");
		try{
 	  		func.createBook(new BookDTO(ISBN,publisher,title,pages),authorID,subjectID);			
		}catch(DALException e){
			out.print(e.getMessage());
		}
		}
	}
%>
<div class=tabledesign style="float:left;">
<%	
List<PublisherDTO> pubList = func.getPublisherIdList();
List<AuthorDTO> autList = func.getAuthorIdList();
List<SubjectDTO> subList = func.getSubjectIdList();

	out.print("<table id=\"tabledesign\">");
	out.print("<tr>");
	out.print("<td>ISBN </td>");
	out.print("<td>Publisher ID </td>");
	out.print("<td>Title </td>");
	out.print("<td>Pages</td>");
	out.print("<td>Author ID </td>");
	out.print("<td>Subject ID</td>");
	out.print("</tr>");
		out.print("<tr>");
		out.print("<td>"+"<input type=\"text\" name=\"ISBN\" value=\"\" size=2>"+"</td>"); 
		out.print("<td>"+"<select name=\"publisherID\">");
			out.print("<option value=null selected>Pick ID</option>");
			for(int x=0; x<pubList.size(); x++){
				out.print("<option value="+(x+1)+">"+pubList.get(x).getPublisher_name()+"</option>");
			}
		out.print("</select></td>"); 
		out.print("<td>"+"<input type=\"text\" name=\"title\" value=\"\" size=2>"+"</td>"); 
		out.print("<td>"+"<input type=\"text\" name=\"pages\" value=\"\" size=2>"+"</td>");
		out.print("<td>"+"<select name=\"authorID\">");
			out.print("<option value=null selected>Pick ID</option>");
			for(int y=0; y<autList.size(); y++){
				out.print("<option value="+(y+1)+">"+autList.get(y).getAuthorFirstName()+" "+autList.get(y).getAuthorLastName()+"</option>");
			}
		out.print("<td>"+"<select name=\"subjectID\">");
			out.print("<option value=null selected>Pick ID</option>");
			for(int z=0;z<subList.size(); z++){
				out.print("<option value="+(z+1)+">"+subList.get(z).getSubject_name()+"</option>");
			}
		out.print("</tr>");
	out.print("</table>");
	
%>

</div>
<br><input type="submit" name="action" value="Submit">
	
		</form>
		
			</div>
		${userMENU}
		</div>
	</div>

<div id="loginField" class="window">
	<form action="../LoginServlet">
			Username:
			<input type="text" name="un"><br>
			Password:
			<input type="text" name="pw"><br>
			<input type="submit" value="Log In">
		</form>	
		<br>
	<a href="#"class="close" align=right style="color: #333;text-align: right;text-decoration: none;">Close</a>
</div>	
<div id="mask"></div>

</body>
</html>